//
//  SRSelectDateView.m
//  SportRing
//
//  Created by icelee on 14-10-28.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSelectDateView.h"

const float btnWidth = 30.0f;

@interface SRSelectDateView()

@property (nonatomic, strong)NSDate *currentDate;

@property (nonatomic, assign)NSInteger addDay;

@property (nonatomic, strong)UILabel *dateLabel;

@end

@implementation SRSelectDateView

- (void)awakeFromNib{
    [super awakeFromNib];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)setDateType:(SELECTE_DATE_TYPE)dateType
{
    if (_dateType == dateType) {
        return;
    }
    _dateType = dateType;
    
    [self inits];

}

- (void)inits{
    
    _addDay = 0;
    
    _currentDate = [NSDate date];
    _maxSelectedDate = [NSDate date];
    
    _selectedDate = [[NSDate alloc] init];
    
    _selectedDate = _currentDate;
    
    UIButton *previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [previousButton setBackgroundImage:[UIImage imageNamed:@"select_left"] forState:UIControlStateNormal];
    [previousButton setFrame:CGRectMake(0, 0, btnWidth, self.frame.size.height)];
    [previousButton addTarget:self action:@selector(previousButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:previousButton];
    
    _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(btnWidth, 0, self.frame.size.width-2*btnWidth, self.frame.size.height)];
    
    switch (self.dateType) {
        case SELECTE_DATE_TYPE_MONTH_DAY:
            
            _dateLabel.text = [self formatDateString:[NSDate date] withType:SELECTE_DATE_TYPE_MONTH_DAY];
            
            break;
        case SELECTE_DATE_TYPE_YEAR_MONTH_DAY:
            
            _dateLabel.text = [self formatDateString:[NSDate date] withType:SELECTE_DATE_TYPE_MONTH_DAY];
            
            break;
        case SELECTE_DATE_TYPE_YEAR:
            
            _dateLabel.text = [self formatDateString:[NSDate date] withType:SELECTE_DATE_TYPE_MONTH_DAY];
            
            break;
        case SELECTE_DATE_TYPE_MONTH:
            
            _dateLabel.text = [self formatDateString:[NSDate date] withType:SELECTE_DATE_TYPE_MONTH_DAY];
            
            break;
        case SELECTE_DATE_TYPE_DAY:
            
            _dateLabel.text = [self formatDateString:[NSDate date] withType:SELECTE_DATE_TYPE_MONTH_DAY];
            
            break;
            
        default:
            break;
    }
    
    [_dateLabel setFont:[UIFont systemFontOfSize:12]];
    
    _dateLabel.backgroundColor = [UIColor clearColor];
    
    _dateLabel.textColor = [UIColor blackColor];
    
    _dateLabel.textAlignment = NSTextAlignmentCenter;
    
    _selectedDateString = [self formatDateString:[NSDate date] withType:self.dateType];
    
    [self addSubview:_dateLabel];
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"select_right"] forState:UIControlStateNormal];
    [nextButton setFrame:CGRectMake(self.frame.size.width-btnWidth, 0, btnWidth, self.frame.size.height)];
    [nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:nextButton];
}

- (NSString *)formatDateString:(NSDate *)date withType:(SELECTE_DATE_TYPE)dateType
{
    NSString* dateString;
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    
    switch (self.dateType) {
            
        case SELECTE_DATE_TYPE_MONTH_DAY:
            
            [formatter setDateFormat:@"MM月dd日"];
            
            break;
        case SELECTE_DATE_TYPE_YEAR_MONTH_DAY:
            
            [formatter setDateFormat:@"yyyy年MM月dd日"];
            
            break;
        case SELECTE_DATE_TYPE_YEAR:
            
            [formatter setDateFormat:@"yyyy年"];
            
            break;
        case SELECTE_DATE_TYPE_MONTH:
            
            [formatter setDateFormat:@"MM月"];
            
            break;
        case SELECTE_DATE_TYPE_DAY:
            
            [formatter setDateFormat:@"dd日"];
            
            break;
            
        default:
            break;
    }
    
    dateString = [formatter stringFromDate:date];
    
    return dateString;
}

- (void)today{
    
    [_dateLabel setText:[self formatDateString:[NSDate date] withType:self.dateType]];
    
    _selectedDate = [NSDate date];
    
    _addDay = 0;
    
}

- (void)realSelectedDate:(NSDate *)selectedDate
{
    
}

- (void)previousButtonAction:(id)sender
{
    
    switch (self.dateType) {
            
        case SELECTE_DATE_TYPE_MONTH_DAY:
        case SELECTE_DATE_TYPE_YEAR_MONTH_DAY:
        case SELECTE_DATE_TYPE_DAY:
            
            _addDay--;
            _selectedDate = [_currentDate dateByAddingTimeInterval:_addDay*3600*24];
            break;
            
        case SELECTE_DATE_TYPE_YEAR:
            
            _selectedDate = [_selectedDate dateByMovingToFirstDayOfThePreviousYear];
            
            break;
        case SELECTE_DATE_TYPE_MONTH:
            
            _selectedDate = [_selectedDate dateByMovingToFirstDayOfThePreviousMonth];
            break;
            
        default:
            break;
    }
    
    _selectedDateString = [self formatDateString:_selectedDate withType:self.dateType];
    
    [_dateLabel setText:_selectedDateString];
 
    if (_srSelectDelegate && [_srSelectDelegate respondsToSelector:@selector(selectedDate:)]) {
        
        NSLog(@"selected year:%ld month:%ld day:%ld",[_selectedDate year],[_selectedDate month],[_selectedDate day]);
        
        [_srSelectDelegate selectedDate:self];
    }
    
}

- (void)nextButtonAction:(id)sender
{
    switch (self.dateType) {
            
        case SELECTE_DATE_TYPE_MONTH_DAY:
        case SELECTE_DATE_TYPE_DAY:
        case SELECTE_DATE_TYPE_YEAR_MONTH_DAY:
            
            //如果最大天数和当前天数的相同
            if ([_selectedDate isSameDay:_maxSelectedDate])
            {
                _selectedDate = self.maxSelectedDate;
            }
            else
            {
                _addDay++;
                _selectedDate = [_currentDate dateByAddingTimeInterval:_addDay*3600*24];
            }
            break;
            
        case SELECTE_DATE_TYPE_YEAR:
            
            _selectedDate = [_selectedDate dateByMovingToFirstDayOfTheFollowingYear];
            
            break;
        case SELECTE_DATE_TYPE_MONTH:
            
            //如果当前月份和最大天数的月份相同
            if (_selectedDate.month == self.maxSelectedDate.month)
            {
                _selectedDate = [_selectedDate dateByMovingToFirstDayOfTheMonth];
            }
            else
                _selectedDate = [_selectedDate dateByMovingToFirstDayOfTheFollowingMonth];
            
            break;
            
        default:
            break;
    }
    
    _selectedDateString = [self formatDateString:_selectedDate withType:self.dateType];
    
    [_dateLabel setText:_selectedDateString];
    
    if (_srSelectDelegate && [_srSelectDelegate respondsToSelector:@selector(selectedDate:)]) {
        
        NSLog(@"selected year:%ld month:%ld day:%ld",[_selectedDate year],[_selectedDate month],[_selectedDate day]);
        
        [_srSelectDelegate selectedDate:self];
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
